package com.kurumi.util;

import java.awt.Font;
import java.awt.FontMetrics;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.extgstate.PdfExtGState;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.VerticalAlignment;

public class PdfWaterMarkUtil {
	
	/*public static void main(String[] args) throws Exception{
		String filePath = "C:\\Users\\h2oco2\\Desktop\\xxxx.PDF";
		List<WaterMarkPosition> wmpList = new ArrayList<WaterMarkPosition>();
		WaterMarkPosition w1 = new WaterMarkPosition();
		w1.setAngle(-170);
		w1.setFontSize(30);
		w1.setPosition(WaterMarkPosition.LEFT_CENTER);
		w1.setText("毓璜顶医院");
		w1.setX(200);
		w1.setY(0);
		wmpList.add(w1);
		
		WaterMarkPosition w2 = new WaterMarkPosition();
		w1.setAngle(-170);
		w1.setFontSize(30);
		w1.setPosition(WaterMarkPosition.TOP_CENTER);
		w1.setText("毓璜顶医院");
		w1.setX(0);
		w1.setY(200);
		wmpList.add(w2);
		
		byte[] b = getPdfMarkByText(FileUtil.toByteArray(filePath), "毓璜顶医院", null, 30, WaterMarkPosition.LEFT_CENTER, 200, 0, -170);
		
		//姓名
		//b = getPdfMarkByText(b, "张三", null, 16, WaterMarkPosition.LEFT_BUTTOM, 350, 70, 0);
		
		//创建日期
		b = getPdfMarkByText(b, "2019-10-12 10:01:10", null, 16, WaterMarkPosition.LEFT_BUTTOM, 350, 50, 0);
		
		//ip
		b = getPdfMarkByText(b, "192.168.4.170", null, 16, WaterMarkPosition.LEFT_BUTTOM, 350, 30, 0);
		
		
		FileOutputStream out = new FileOutputStream(new File("C:\\Users\\h2oco2\\Desktop\\xxxx1.PDF"));
		out.write(b);
		out.flush();
		out.close();
		b = null;
	}
	*/
	public static byte[] createPrintMedicalPdfWater(byte [] b, String hospitalName, String patientName, String createTimeFormat, String ipAddress) throws Exception{
		b = getPdfMarkByText(b, hospitalName, null, 30, WaterMarkPosition.LEFT_CENTER, 200, 0, -170);
		
		//姓名
		//b = getPdfMarkByText(b, patientName, null, 16, WaterMarkPosition.LEFT_BUTTOM, 350, 70, 0);
		
		//创建日期
		b = getPdfMarkByText(b, createTimeFormat, null, 16, WaterMarkPosition.LEFT_BUTTOM, 350, 50, 0);
		
		//ip
		b = getPdfMarkByText(b, ipAddress, null, 16, WaterMarkPosition.LEFT_BUTTOM, 350, 30, 0);
		
		
		return b;
	}
	
	
	
	/**
	 * 给pdf添加文字水印
	 * @param pdf
	 * @param text
	 * @return
	 */
	public static byte[] getPdfMarkByText(byte[] pdf, String text, Color fontColor, int fontSize, float x, float y, float angle) throws Exception{
		InputStream sourceInput = new ByteArrayInputStream(pdf);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		
		try {
			PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceInput), new PdfWriter(out));
			Document doc = new Document(pdfDoc);
			int n = pdfDoc.getNumberOfPages();
			PdfFont font = FontManager.getSimSunFont();
			Paragraph p = new Paragraph(text).setFont(font).setFontSize(30);
			p.setFontColor(fontColor);
			PdfExtGState gs1 = new PdfExtGState();
			gs1.setFillOpacity(0.5f);
			PdfCanvas over;
			
			for (int i = 1; i <= n; i++) {
				PdfPage pdfPage = pdfDoc.getPage(i);
				pdfPage.setIgnorePageRotationForContent(true);

				over = new PdfCanvas(pdfDoc.getPage(i));
				over.saveState();
				over.setExtGState(gs1);
				doc.showTextAligned(p, x, y, i, TextAlignment.LEFT, VerticalAlignment.MIDDLE, angle);
				over.restoreState();
			}
			
			doc.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return out.toByteArray();
	}
	
	
	/**
	 * 给pdf添加文字水印 相对位置
	 * @param pdf
	 * @param text
	 * @return
	 */
	public static byte[] getPdfMarkByText(byte[] pdf, String text, Color fontColor, int fontSize, int position, float xOffset, float yOffset, float angle)
		throws Exception{
		InputStream sourceInput = null;
		ByteArrayOutputStream out = null;
		Document doc = null;
		
		try {
			sourceInput = new ByteArrayInputStream(pdf);
			out = new ByteArrayOutputStream();
			PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceInput), new PdfWriter(out));
			doc = new Document(pdfDoc);
			int n = pdfDoc.getNumberOfPages();
			PdfFont font = FontManager.getSimSunFont();
			Paragraph p = new Paragraph(text).setFont(font).setFontSize(fontSize);
			p.setFontColor(fontColor);
			PdfExtGState gs1 = new PdfExtGState();
			gs1.setFillOpacity(0.2f);
			PdfCanvas over;
			Rectangle pagesize;
			Font f = new Font("宋体", Font.PLAIN, fontSize);    
		    FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(f);    
			//无法获取的文本的宽度和高度
			float markWidth = fm.stringWidth(text);
			float markHeight = fm.getHeight();
			float x, y;
			
			for (int i = 1; i <= n; i++) {
				PdfPage pdfPage = pdfDoc.getPage(i);
				pagesize = pdfPage.getPageSizeWithRotation();
				pdfPage.setIgnorePageRotationForContent(true);

		    	float imageWidth = pagesize.getWidth();
				float imageHeight = pagesize.getHeight();

				WaterMarkPosition markPosition = WaterMarkPosition.getPdfWaterMarkPosition(position, xOffset, yOffset, imageWidth, imageHeight, markWidth, markHeight);
		    	
				if(markPosition == null){
		    		//默认左下
		    		markPosition = WaterMarkPosition.getPdfWaterMarkPosition(WaterMarkPosition.LEFT_BUTTOM, xOffset, yOffset, imageWidth, imageHeight, markWidth, markHeight);
		    	}
		    	
	    		x = markPosition.getX();
	    		y = markPosition.getY();
				over = new PdfCanvas(pdfDoc.getPage(i));
				over.saveState();
				over.setExtGState(gs1);
				doc.showTextAligned(p, x, y, i, TextAlignment.LEFT, VerticalAlignment.MIDDLE, angle);
				over.restoreState();
			}
			
			doc.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != out) {
				out.close();
			}
			
			if (null != sourceInput) {
				sourceInput.close();
			}
			
			if (null != doc) {
				doc.close();
			}
			
			//释放内存
			pdf = null;
		}
		return out.toByteArray();
	}
	
	
	
	/**
	 * 给图片添加图片水印 
	 * @param pdf
	 * @param image
	 * @return
	 */
	public static byte[] getPdfMarkByImage(byte[] pdf, byte[] image, float x, float y){
		InputStream sourceInput = new ByteArrayInputStream(pdf);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceInput), new PdfWriter(out));
			Document doc = new Document(pdfDoc);
			int n = pdfDoc.getNumberOfPages();
			ImageData img = ImageDataFactory.create(image);
			float w = img.getWidth();
			float h = img.getHeight();
			PdfExtGState gs1 = new PdfExtGState();
			gs1.setFillOpacity(0.5f);
			PdfCanvas over;
			for (int i = 1; i <= n; i++) {
				PdfPage pdfPage = pdfDoc.getPage(i);
				pdfPage.setIgnorePageRotationForContent(true);

				over = new PdfCanvas(pdfDoc.getPage(i));
				over.saveState();
				over.setExtGState(gs1);
				over.addImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2), false);
				over.restoreState();
			}
			doc.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return out.toByteArray();
	}
	
	/**
	 * 给图片添加图片水印 相对位置
	 * @param pdf
	 * @param image
	 * @return
	 */
	public static byte[] getPdfMarkByImage(byte[] pdf, byte[] image, int position, float xOffset, float yOffset){
		InputStream sourceInput = new ByteArrayInputStream(pdf);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceInput), new PdfWriter(out));
			Document doc = new Document(pdfDoc);
			int n = pdfDoc.getNumberOfPages();
			ImageData img = ImageDataFactory.create(image);
			float markWidth = img.getWidth();
			float markHeight = img.getHeight();
			PdfExtGState gs1 = new PdfExtGState();
			gs1.setFillOpacity(0.5f);
			PdfCanvas over;
			Rectangle pagesize;
			float x, y;
			for (int i = 1; i <= n; i++) {
				PdfPage pdfPage = pdfDoc.getPage(i);
				pagesize = pdfPage.getPageSizeWithRotation();
				pdfPage.setIgnorePageRotationForContent(true);
				
				float imageWidth = pagesize.getWidth();
				float imageHeight = pagesize.getHeight();
				WaterMarkPosition markPosition = WaterMarkPosition.getPdfWaterMarkPosition(position, xOffset, yOffset, imageWidth, imageHeight, markWidth, markHeight);
		    	if(markPosition == null){
		    		//默认左下
		    		markPosition = WaterMarkPosition.getPdfWaterMarkPosition(WaterMarkPosition.LEFT_BUTTOM, xOffset, yOffset, imageWidth, imageHeight, markWidth, markHeight);
		    	}
	    		x = markPosition.getX();
	    		y = markPosition.getY();
				over = new PdfCanvas(pdfDoc.getPage(i));
				over.saveState();
				over.setExtGState(gs1);
				over.addImage(img, markWidth, 0, 0, markHeight, x, y, false);
				over.restoreState();
			}
			doc.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return out.toByteArray();
	}
}
